//
//  MainViewController.m
//  Mp4Writer
//
//  Created by zjp on 2021/8/23.
//

#import "MainViewController.h"
#import <TXLiteAVSDK_TRTC/TRTCCloud.h>
#import "Debug/GenerateTestUserSig.h"
#import "RoomData.h"

@interface MainViewController () <TRTCCloudDelegate, TRTCVideoRenderDelegate, TRTCVideoFrameDelegate, TRTCAudioFrameDelegate>

@property (weak, nonatomic) IBOutlet UIView *localView;
@property (weak, nonatomic) IBOutlet UIView *remoteView;
@property (weak, nonatomic) IBOutlet UIButton *startRecordBtn;
@property (weak, nonatomic) IBOutlet UIButton *endRecordBtn;

@property (nonatomic) TRTCCloud* trtcCloud;

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupTRTCCloud];
    self.remoteView.hidden = YES;
    
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NSString* str = (NSString*)sender;
    NSLog(@"%@", str);
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    NSLog(@"viewDidDisappear");
    
    [self destroyTRTCCloud];
}

#pragma mark - TRTCCloud

- (void)setupTRTCCloud {
    [TRTCCloud setLogLevel:TRTCLogLevelDebug];
    [TRTCCloud setConsoleEnabled:YES];
    
    self.trtcCloud = [TRTCCloud sharedInstance];
    
    self.trtcCloud.delegate = self;
    
    [self.trtcCloud setAudioFrameDelegate:self];
    
    [self.trtcCloud setLocalVideoProcessDelegete:self pixelFormat:TRTCVideoPixelFormat_NV12 bufferType:TRTCVideoBufferType_PixelBuffer];
    
    [self.trtcCloud startLocalPreview:YES view:self.localView];
    
    TRTCParams *params = [TRTCParams new];
    params.sdkAppId = SDKAppID;
    params.roomId = [[RoomData sharedInstance].roomid intValue];
    params.userId = [RoomData sharedInstance].userid;
    params.role = TRTCRoleAnchor;
    params.userSig = [GenerateTestUserSig genTestUserSig:params.userId];
    
    [self.trtcCloud enterRoom:params appScene:TRTCAppSceneVideoCall];
    
    TRTCVideoEncParam *videoEncParam = [TRTCVideoEncParam new];
    
    videoEncParam.videoResolution = TRTCVideoResolution_160_90;
    videoEncParam.videoBitrate = 1500;
    videoEncParam.videoFps = 15;
    
    [self.trtcCloud setVideoEncoderParam:videoEncParam];
    [self.trtcCloud startLocalAudio:TRTCAudioQualityDefault];
}

- (void)destroyTRTCCloud {
    [self.trtcCloud stopLocalPreview];
    [self.trtcCloud exitRoom];
    [TRTCCloud destroySharedIntance];
    _trtcCloud = nil;
}

#pragma mark - TRTCCloudDelegate

// 第三方美颜接口
- (uint32_t)onProcessVideoFrame:(TRTCVideoFrame *)srcFrame dstFrame:(TRTCVideoFrame *)dstFrame {
    
}


- (void)onUserVideoAvailable:(NSString *)userId available:(BOOL)available {
    if (available) {
        self.remoteView.hidden = NO;
        [self.trtcCloud startRemoteView:userId streamType:TRTCVideoStreamTypeBig view:self.remoteView];
        int suc = [self.trtcCloud setRemoteVideoRenderDelegate:userId delegate:self pixelFormat:TRTCVideoPixelFormat_NV12 bufferType:TRTCVideoBufferType_PixelBuffer];
        printf("suc: %d", suc);
    }
//    else {
//        self.remoteView.hidden = YES;
//        [self.trtcCloud stopRemoteView:userId streamType:TRTCVideoStreamTypeBig];
//    }
}

- (void)onEnterRoom:(NSInteger)result {
    
}

- (void)onExitRoom:(NSInteger)reason {
    
}


@end
